package net.mcreator.magicio.procedures;

import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;

import net.minecraft.world.server.ServerWorld;
import net.minecraft.world.IWorld;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.math.vector.Vector2f;
import net.minecraft.util.ResourceLocation;
import net.minecraft.entity.Entity;
import net.minecraft.command.ICommandSource;
import net.minecraft.command.FunctionObject;
import net.minecraft.command.CommandSource;

import net.mcreator.magicio.MagicIoModVariables;
import net.mcreator.magicio.MagicIoMod;

import java.util.Optional;
import java.util.Map;
import java.util.HashMap;

public class InitializeProcedure {
	@Mod.EventBusSubscriber
	private static class GlobalTrigger {
		@SubscribeEvent
		public static void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) {
			Entity entity = event.getPlayer();
			Map<String, Object> dependencies = new HashMap<>();
			dependencies.put("x", entity.getPosX());
			dependencies.put("y", entity.getPosY());
			dependencies.put("z", entity.getPosZ());
			dependencies.put("world", entity.world);
			dependencies.put("entity", entity);
			dependencies.put("event", event);
			executeProcedure(dependencies);
		}
	}
	public static void executeProcedure(Map<String, Object> dependencies) {
		if (dependencies.get("entity") == null) {
			if (!dependencies.containsKey("entity"))
				MagicIoMod.LOGGER.warn("Failed to load dependency entity for procedure Initialize!");
			return;
		}
		if (dependencies.get("x") == null) {
			if (!dependencies.containsKey("x"))
				MagicIoMod.LOGGER.warn("Failed to load dependency x for procedure Initialize!");
			return;
		}
		if (dependencies.get("y") == null) {
			if (!dependencies.containsKey("y"))
				MagicIoMod.LOGGER.warn("Failed to load dependency y for procedure Initialize!");
			return;
		}
		if (dependencies.get("z") == null) {
			if (!dependencies.containsKey("z"))
				MagicIoMod.LOGGER.warn("Failed to load dependency z for procedure Initialize!");
			return;
		}
		if (dependencies.get("world") == null) {
			if (!dependencies.containsKey("world"))
				MagicIoMod.LOGGER.warn("Failed to load dependency world for procedure Initialize!");
			return;
		}
		Entity entity = (Entity) dependencies.get("entity");
		double x = dependencies.get("x") instanceof Integer ? (int) dependencies.get("x") : (double) dependencies.get("x");
		double y = dependencies.get("y") instanceof Integer ? (int) dependencies.get("y") : (double) dependencies.get("y");
		double z = dependencies.get("z") instanceof Integer ? (int) dependencies.get("z") : (double) dependencies.get("z");
		IWorld world = (IWorld) dependencies.get("world");
		if (world instanceof ServerWorld && ((ServerWorld) world).getServer() != null) {
			Optional<FunctionObject> _fopt = ((ServerWorld) world).getServer().getFunctionManager().get(new ResourceLocation("magic_io:team"));
			if (_fopt.isPresent()) {
				FunctionObject _fobj = _fopt.get();
				((ServerWorld) world).getServer().getFunctionManager().execute(_fobj, new CommandSource(ICommandSource.DUMMY, new Vector3d(x, y, z),
						Vector2f.ZERO, (ServerWorld) world, 4, "", new StringTextComponent(""), ((ServerWorld) world).getServer(), null));
			}
		}
		if (MagicIoModVariables.MapVariables.get(world).initialize) {
			entity.getPersistentData().putDouble("level_fire", 1);
			entity.getPersistentData().putDouble("level_water", 1);
			entity.getPersistentData().putDouble("level_dirt", 1);
			entity.getPersistentData().putDouble("level_wind", 1);
			entity.getPersistentData().putDouble("level_wood", 1);
			entity.getPersistentData().putDouble("level_light", 1);
			entity.getPersistentData().putDouble("level_gold", 1);
			MagicIoModVariables.MapVariables.get(world).initialize = (boolean) (false);
			MagicIoModVariables.MapVariables.get(world).syncData(world);
		}
	}
}
